package gatperdut.chato.core.interpreter.actions;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.constants.CSConstantsMovement.Direction;
import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.core.characters.BaseController;
import gatperdut.chato.core.interpreter.actions.generic.Action;
import gatperdut.chato.core.interpreter.grammar.comminfo.ExitsInfo;
import gatperdut.chato.core.room.DoorController;
import gatperdut.chato.core.room.RoomController;

import java.util.Map;
import java.util.Map.Entry;

public class ExitsAction extends Action {

    public ExitsAction( ExitsInfo info, BaseController entityController ) {
        super( info, entityController );
    }

    @Override
    public CommandExecutionResult perform() {
        checkForRunningCraft();

        Map<Direction, RoomController> rcs = getEntityController().getRoomController().getExitsController().getAdjacentRoomControllers();
        if ( rcs.isEmpty() ) {
            getEntityController().tx( "There are no obvious exits." );
            return CommandExecutionResult.OK;
        }

        Map<Direction, DoorController> dcs = getEntityController().getRoomController().getExitsController().getAdjacentDoorControllers();

        StringBuilder sb = new StringBuilder( "The following exits are obvious:" + CSConstantsString.NEWLINE );

        Direction d;
        RoomController rc;
        DoorController dc;
        for ( Entry<Direction, RoomController> entry : rcs.entrySet() ) {
            d = entry.getKey();
            rc = entry.getValue();
            dc = dcs.get( d );

            sb.append( d.getAsString() + " " );
            if ( dc == null ) {
                sb.append( "- " + rc.getRoom().getTitle() + CSConstantsString.NEWLINE );
            }
            else {
                sb.append( "(" + dc.getDoor().getDesc() + " - " );
                if ( dc.getDoor().isOpen() ) {
                    sb.append( "open) - " + rc.getRoom().getTitle() );
                }
                else {
                    sb.append( "closed) - " );
                    if ( dc.getDoor().isSeeThrough() ) {
                        sb.append( rc.getRoom().getTitle() );
                    }
                    else {
                        sb.append( "?" );
                    }
                }
                sb.append( CSConstantsString.NEWLINE );
            }
        }

        getEntityController().tx( sb.toString() );

        return CommandExecutionResult.OK;
    }

    @Override
    public boolean interruptsCrafting() {
        return false;
    }

    @Override
    public ExitsInfo getInfo() {
        return (ExitsInfo) super.getInfo();
    }
}
